/*
 * Copyright (c) 2011-2013, Peter Abeles. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package boofcv.core.image;

import boofcv.misc.AutoTypeImage;
import boofcv.misc.CodeGeneratorBase;

import java.io.FileNotFoundException;


/**
 * @author Peter Abeles
 */
public class GenerateConvertImage extends CodeGeneratorBase {

	String className = "ConvertImage";

	public GenerateConvertImage() throws FileNotFoundException {
		setOutputFile(className);
	}

	@Override
	public void generate() throws FileNotFoundException {
		printPreamble();

		for( AutoTypeImage in : AutoTypeImage.getSpecificTypes()) {
			for( AutoTypeImage out : AutoTypeImage.getSpecificTypes() ) {
				if( in == out )
					continue;

				printConvert(in,out);
			}
			printAverage(in);
		}

		out.print("\n" +
				"}\n");
	}

	private void printPreamble() {
		out.print("import boofcv.alg.InputSanityCheck;\n" +
				"import boofcv.core.image.impl.ImplConvertImage;\n" +
				"import boofcv.core.image.impl.ImplConvertMsToSingle;\n" +
				"import boofcv.struct.image.*;\n" +
				"\n" +
				"/**\n" +
				" * <p>\n" +
				" * Functions for converting between different image types. Pixel values are converted by typecasting.\n" +
				" * When converting between signed and unsigned types, care should be taken to avoid numerical overflow.\n" +
				" * </p>\n" +
				" *\n" +
				" * <p>\n" +
				" * DO NOT MODIFY: This class was automatically generated by {@link "+getClass().getCanonicalName()+"}\n" +
				" * </p>\n" +
				" *\n" +
				" * @author Peter Abeles\n" +
				" */\n" +
				"public class "+className+" {\n\n");
	}

	private void printConvert( AutoTypeImage imageIn , AutoTypeImage imageOut ) {

		out.print("\t/**\n" +
				"\t * <p>\n" +
				"\t * Converts an {@link boofcv.struct.image."+imageIn.getSingleBandName()+"} into a {@link boofcv.struct.image."+imageOut.getSingleBandName()+"}.\n" +
				"\t * </p>\n" +
				"\t *\n" +
				"\t * @param input Input image which is being converted. Not modified.\n" +
				"\t * @param output (Optional) The output image.  If null a new image is created. Modified.\n" +
				"\t * @return Converted image.\n" +
				"\t */\n" +
				"\tpublic static "+imageOut.getSingleBandName()+" convert("+imageIn.getSingleBandName()+" input, "+imageOut.getSingleBandName()+" output) {\n" +
				"\t\tif (output == null) {\n" +
				"\t\t\toutput = new "+imageOut.getSingleBandName()+"(input.width, input.height);\n" +
				"\t\t} else {\n" +
				"\t\t\tInputSanityCheck.checkSameShape(input, output);\n" +
				"\t\t}\n" +
				"\n" +
				"\t\tImplConvertImage.convert(input, output);\n" +
				"\n" +
				"\t\treturn output;\n" +
				"\t}\n\n");
	}

	private void printAverage( AutoTypeImage imageIn ) {

		String imageName = imageIn.getSingleBandName();

		out.print("\t/**\n" +
				"\t * Converts a {@link MultiSpectral} into a {@link ImageSingleBand} by computing the average value of each pixel\n" +
				"\t * across all the bands.\n" +
				"\t * \n" +
				"\t * @param input Input MultiSpectral image that is being converted. Not modified.\n" +
				"\t * @param output (Optional) The single band output image.  If null a new image is created. Modified.\n" +
				"\t * @return Converted image.\n" +
				"\t */\n" +
				"\tpublic static "+imageName+" average( MultiSpectral<"+imageName+"> input , "+imageName+" output ) {\n" +
				"\t\tif (output == null) {\n" +
				"\t\t\toutput = new "+imageName+"(input.width, input.height);\n" +
				"\t\t} else {\n" +
				"\t\t\tInputSanityCheck.checkSameShape(input, output);\n" +
				"\t\t}\n" +
				"\n" +
				"\t\tImplConvertMsToSingle.average(input, output);\n" +
				"\n" +
				"\t\treturn output;\n" +
				"\t}\n");
	}

	public static void main( String args[] ) throws FileNotFoundException {
		GenerateConvertImage app = new GenerateConvertImage();

		app.generate();
	}
}
